{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "class bond(object):\n",
    "    def __init__(self, libor, coupon, t_series):\n",
    "        self.libor=libor\n",
    "        self.coupon=coupon\n",
    "        self.t_series = t_series\n",
    "        self.ntimes=len(self.t_series)\n",
    "        self.pvAvg=0.0\n",
    "\n",
    "    def pv(self):\n",
    "        deltaT= self.t_series[2:]-self.t_series[-1:]\n",
    "        self.cashFlows= self.coupon*deltaT\n",
    "        principal = np.zeros(deltaT)\n",
    "        principal[-1:]=1.0\n",
    "        self.cashFlows += principal\n",
    "        pv = self.cashFlows*self.libor[2:]\n",
    "        self.pvAvg = np.average(pv,axis=2)\n",
    "        return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "__author__ = 'marcopereira'\n",
    "from pandas import DataFrame, Series\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "class MC_Vasicek_Sim(object):\n",
    "    def __init__(self, date_list,r0,sigmaR,muR,alphaR, simNumber,t_step):\n",
    "    #SDE parameters - Vasicek SDE\n",
    "        self.sigmaR = sigmaR\n",
    "        self.muR = muR\n",
    "        self.alphaR = alphaR\n",
    "        self.simNumber = simNumber\n",
    "        self.t_step = t_step\n",
    "        self.r0 = r0\n",
    "    #internal representation of times series - integer multiples of t_step\n",
    "        self.t_series = pd.DataFrame(date_list)\n",
    "    #creation of a fine grid for Monte Carlo integration\n",
    "        #Create fine date grid for SDE integration\n",
    "        minDay = min(date_list)\n",
    "        maxDay = max(date_list)\n",
    "        self.datelistlong = pd.date_range(minDay, maxDay).tolist()\n",
    "        self.ntimes = len(self.datelistlong)\n",
    "        return\n",
    "\n",
    "    def getLibor(self):\n",
    "        rd = np.random.random((self.ntimes,self.simNumber))   # array of numbers for the number of samples\n",
    "        r = np.ones(np.shape(rd))\n",
    "        r[0,:] = self.r0*r[0,:]\n",
    "        nrows = np.shape(rd)[0]\n",
    "        sigmaDT = self.sigmaR* np.sqrt(self.t_step)\n",
    "    #calculate r(t)\n",
    "        r[0,:] = self.r0*r[0,:]\n",
    "        for i in np.arange(1,nrows):\n",
    "            r[i,:] = r[i-1,:]+ self.alphaR*(self.muR-r[i-1,:]) + sigmaDT*rd[i,:]\n",
    "    #calculate integral(r(s)ds)\n",
    "        integralR = r.cumprod(axis=0)*self.t_step\n",
    "    #calculate Libor\n",
    "        self.libor = np.exp(-integralR)\n",
    "        return\n",
    "\n",
    "    def return_indices1_of_a(self, a, b):\n",
    "        b_set = set(b)\n",
    "        return [i for i, v in enumerate(a) if v in b_set]\n",
    "\n",
    "    def return_indices2_of_a(self, a, b):\n",
    "        index=[]\n",
    "        for item in a:\n",
    "            index.append(np.bisect.bisect(b,item))\n",
    "        return np.unique(index).tolist()\n",
    "\n",
    "    def getSmallLibor(self):\n",
    "        #calculate indexes\n",
    "        ind = self.return1_indices(self.date_list, self.datelistlong)\n",
    "        return self.libor[ind,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "__author__ = 'marcopereira'\n",
    "import numpy as np\n",
    "import datetime\n",
    "\n",
    "\n",
    "#CashFlow Dates\n",
    "t_series = np.round(np.arange(0,0.25001,0.25)*365)\n",
    "base = datetime.datetime.today()\n",
    "datelist = [base + datetime.timedelta(days=x) for x in t_series]\n",
    "\n",
    "#SDE parameter\n",
    "t_step = 1.0/365\n",
    "r0 = 0.08\n",
    "sigmaR = 0.05\n",
    "muR = 0.05\n",
    "alphaR=3.0\n",
    "simNumber=10\n",
    "\n",
    "#Bond parameters\n",
    "coupon = 0.08"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#Monte Carlo trajectories creation\n",
    "myVasicek = MC_Vasicek_Sim(datelist, r0,sigmaR, muR, alphaR, simNumber,t_step).getLibor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "libor = myVasicek.getSmallLibor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "myVasicek"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}